<?php

namespace app\admin\controller;

use think\Db;
use think\Controller;
use think\Request;
use think\Session;

class Actor extends AdminController
{
    public function _initialize()
    {
        parent::_initialize();
    }
    // public function index()
    // {
    //     return view('admin@con1/tables');
    // }

    public function index()
    {
        // 读取内容列表
        // $data = Db::table('sm_h_actor')->select();
//        $icon = $data[0]['cover'];
//        $a = substr($icon,0,strpos($icon, '_'));
//        $b = substr($icon,strpos($icon,'_')+1);
        $data = Db::table('sm_h_actor')->paginate(5);
        $page = $data->render();
        return view('admin@Actor/index', [
            'data' => $data,
            'page' => $page
        ]);
    }

    public function add()
    {
        return view('admin@Actor/add', [
            'title' => '演员添加'
        ]);
    }

    // 添加内容
    public function insert(Request $request)
    {
        $data = $request->post();
        $file = request()->file('face');
        if (empty($file)) {
            $this->error('请添加照片 ', url('admin/Actor/add'));
        }

        $d['name'] = $data['name'];
        $d['sex'] = $data['sex'];
        $d['country'] = $data['country'];
        $d['describe'] = $data['describe'];
        $d['ayear'] = $data['ayear'];

        if (isset($file)) {
            // 获取表单上传文件 例如上传了001.jpg
            // 移动到框架应用根目录/public/uploads/ 目录下
            $info = $file->move(ROOT_PATH . 'public/uploads');
//       var_dump($info) ;die;

            if ($info) {
                // 成功上传后 获取上传信息
                $a = $info->getSaveName();
                $face = str_replace("\\", "/", $a);

                $s = [
                    'name' => $d['name'],
                    'sex' => $d['sex'],
                    'country' => $d['country'],
                    'describe' => $d['describe'],
                    'ayear' => $d['ayear'],
                    'face' => $face
                ];

                $result = Db('h_actor')->insert($s);
                if ($result) {
                    return $this->success('新增成功', 'Actor/index');
                } else {
                    return $this->error('新增失败');
                }

            } else {
                // 上传失败获取错误信息
                echo $file->getError();
            }
        }


//    var_dump($data);die;


    }

    public function delete($id)
    {
//         echo $id;
        $result = db('h_actor')->delete($id);
        if ($result > 0) {
            $info['status'] = true;
        } else {
            $info['status'] = false;
        }
        return json($info);

    }

    public function edit($id)
    {
        $data = db('h_actor')->find($id);
        $imgs = $data['face'];
        return view('admin@Actor/edit', [
            'title' => '我是修改ID:' . $id . '的演员页面',
            'data' => $data,
            'id' => $id,
            'imgs' => $imgs
        ]);
    }

    public function update(Request $request)
    {
        $data = $request->post();

        $file = request()->file('face');
        $id = $data['id'];
        if (isset($file)) {

            // 获取表单上传文件 例如上传了001.jpg
            // 移动到框架应用根目录/public/uploads/ 目录下
            $info = $file->move(ROOT_PATH . 'public/uploads');
//       var_dump($info) ;die;

            if ($info) {
                // 成功上传后 获取上传信息
                $a = $info->getSaveName();
                $face = str_replace("\\", "/", $a);
                $b = [
                    'name' => $data['name'],
                    'sex' => $data['sex'],
                    'country' => $data['country'],
                    'describe' => $data['describe'],
                    'ayear' => $data['ayear'],
                    'face' => $face
                ];

                $result = Db('h_actor')->where('id', $id)->update($b);

                if ($result) {
                    return $this->success('编辑成功', 'Actor/index');
                } else {
                    return $this->error('编辑失败');
                }

            }
        } elseif (empty($file)) {
            $b = [
                'name' => $data['name'],
                'sex' => $data['sex'],
                'country' => $data['country'],
                'describe' => $data['describe'],
                'ayear' => $data['ayear']
            ];

            $result = Db('h_actor')->where('id', $id)->update($b);

            if ($result) {
                return $this->success('编辑成功', 'Actor/index');
            } else {
                return $this->error('编辑失败');
            }

        } else {

            // 上传失败获取错误信息
            echo $file->getError();
        }


    }



}
